package com.example.administrator.capital.utils;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Toast;

import com.example.administrator.capital.App;

/**
 * Created by ${ZhouZiWen} on 2017/9/19.
 * Email:wytaper495@qq.com
 */
public abstract class ToastUtil {

    private static Toast toast;
    private static Toast toast2;

    /**
     * @param context
     * @param layoutId
     * 引用布局的toast(可以更改任意的样式)
     */
    public ToastUtil(Context context, int layoutId, int wight, int height) {
        initView(context,layoutId,wight, height);
    }
    private void initView(Context context, int LayoutId,int wight,int height) {
        toast2 = new Toast(context);
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View BgView = inflater.inflate(LayoutId, null);
        toast2.setView(BgView);
        toast2.setGravity(Gravity.CENTER, wight, height);
        toast2.show();
        //这个方法传了一个view布局 可以在view上找到对应的控件 并赋值
        setData(BgView);
    }
    public abstract void setData(View view);


    private static Toast initToast(CharSequence message, int duration) {
        if (toast == null) {
            toast = Toast.makeText(App.getInstance(), message, duration);
        } else {
            toast.setText(message);
            toast.setDuration(duration);
        }
        return toast;
    }
    /**
     * 短时间显示Toast
     *
     * @param message
     */
    public static void showShort(CharSequence message) {
        initToast(message, Toast.LENGTH_SHORT).show();
    }
    /**
     * 短时间显示Toast
     *
     * @param strResId
     */
    public static void showShort(int strResId) {
        initToast(App.getInstance().getResources().getText(strResId), Toast.LENGTH_SHORT).show();
    }
    /**
     * 长时间显示Toast
     *
     * @param message
     */
    public static void showLong(CharSequence message) {
        initToast(message, Toast.LENGTH_LONG).show();
    }
    /**
     * 长时间显示Toast
     *
     * @param strResId
     */
    public static void showLong(int strResId) {
        initToast(App.getInstance().getResources().getText(strResId), Toast.LENGTH_LONG).show();
    }
    /**
     * 自定义显示Toast时间
     *
     * @param message
     * @param duration
     */
    public static void showCustom(CharSequence message, int duration) {
        initToast(message, duration).show();
    }
    /**
     * 自定义显示Toast时间
     *
     * @param context
     * @param strResId
     * @param duration
     */
    public static void showCustom(Context context, int strResId, int duration) {
        initToast(context.getResources().getText(strResId), duration).show();
    }
}
